
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends MY_Controller {

	public function __construct()
	{
		parent::__construct();
	}

	public function index()
	{
		$this->load->helper(array('form', 'url'));

		if (!$this->is_logged_in()) {
			$this->load->helper(array('form', 'url'));
			$data['title'] = 'Login';
			$this->load->view('templates/header', $data);
			$this->load->view('pages/loginpage');
			$this->load->view('templates/footer');
		} else {
			switch ($_SESSION['role']) {
				case 'admin':
					redirect('admin');
					break;
				case 'student':
					redirect('student');
					break;
				case 'encoder':
					redirect('encoder');
					break;
				case 'instructor':
					redirect('admin');
					break;
			}
		}
	}

	public function process()
	{
		$this->load->helper(array('url','form'));
		$this->load->library('form_validation');
		
		/* Get input of the user. */
		$username = $this->input->post('uname');
		$password = $this->input->post('password');
		$role = $this->input->post('role');

		/* To modify the position of the error messages(?) Paexplore nalang vince :)) */
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); 
		
		/* 1st param is the rule. 2nd param is the the text you want to be displayed. */
		$this->form_validation->set_message('integer', 'Role is required.');

		/* Rules */ 
		$this->form_validation->set_rules('uname','uname','trim|required|callback_username_exists['.$role.']');
		$this->form_validation->set_rules('password','password','trim|required|min_length[6]|max_length[20]');
		$this->form_validation->set_rules('role','role','trim|required|integer');
		
		if($this->form_validation->run() == FALSE) {
			$data['title'] = 'Login';
			$this->load->view('templates/header', $data);
			$this->load->view('pages/loginpage');
			$this->load->view('templates/footer');
		} else {
			
			$data = array(
				'username' => $username,
				'password' => md5($password),
			);
			switch ($role) {
				case 1:
					$this->load->model('admin_model');
					/* Check kung activated na? */
					$result = $this->admin_model->check_login($data);
					break;
				case 2:
					$this->load->model('encoder_model');
					$result = $this->encoder_model->check_login($data);
					break;
				case 3:
					$this->load->model('student_model');
					$result = $this->student_model->check_login($data);
					break;
				case 4:
					$this->load->model('instructor_model');
					$result = $this->instructor_model->check_login($data);
					break;
			}

			if ($result != NULL) {
				$_SESSION['login'] = 1;
				$_SESSION['username'] = $username;

				switch ($role) {
					case 1:
						$_SESSION['role'] = 'admin';
						redirect('admin');
						break;
					case 2:
						$_SESSION['role'] = 'encoder';
						redirect('encoder');
						break;
					case 3:
						$_SESSION['role'] = 'student';
						redirect('student');
						break;
					case 4:
						$_SESSION['role'] = 'instructor';
						redirect('instructor');
						break;
				}
			} else {
				$data['title'] = 'Log In Error';
				$this->load->view('templates/header', $data);
				$this->load->view('pages/login_errorpage');
				$this->load->view('templates/footer');
			}
		}
	}

	public function signup()
	{
		$this->load->helper(array('url','form'));
		$this->load->library('form_validation');
		
		/* Get input of the user. */
		$lname = $this->input->post('lname');
		$fname = $this->input->post('fname');
		$minitial = $this->input->post('minitial');
		$stdno = $this->input->post('stdno');
		$email = $this->input->post('email');
		$username = $this->input->post('username');
		$pw1 = $this->input->post('pw1');
		$pw2 = $this->input->post('pw2');
	
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); 
		$this->form_validation->set_rules('lname','last name','trim|required|callback_alpha_with_space');
		$this->form_validation->set_rules('fname','first name','trim|required|callback_alpha_with_space');
		$this->form_validation->set_rules('minitial','middle initial','trim|required|callback_alpha_with_space');
		$this->form_validation->set_rules('stdno','student number','trim|required|min_length[10]|max_length[10]|callback_stdno_check|callback_valid_std_no');
		$this->form_validation->set_rules('email','email','trim|required|valid_email');
		$this->form_validation->set_rules('username','username','trim|required|callback_username_not_exist[3]');
		$this->form_validation->set_rules('pw1', 'password', 'trim|required|min_length[6]|max_length[20]|matches[pw2]');
		$this->form_validation->set_rules('pw2', 'password confirmation', 'trim|required|min_length[6]|max_length[20]');
		
		if($this->form_validation->run() == FALSE){
			$this->index();
		} else {
			$data = array(
				'name' => $fname . " " . $minitial . " " . $lname,
				'stdno' => $stdno,
				'username' => $username,
				'password' => md5($pw1),
				'approved' => 0,
				'activated' => 0,
				'email' => $email
			);
			$this->load->model('student_model');
			/* Approved and Activated set to zero first. */
			$this->student_model->add_account($data);

			$data['title'] = 'Sign-up Successful!';
			$this->load->view('templates/header', $data);
			$this->load->view('pages/signup_success');
			$this->load->view('templates/footer');
		}
	}
}

/* End of file login.php */
/* Location: ./application/controllers/login.php */